﻿using System;
using System.Web.Mvc;
using Xenta.Utils;
using Xenta.Attributes;
using Xenta.Web.Controllers;

namespace Xenta.Web.Areas.Catalog.Controllers
{
    [Authenticate, InRole("ADMIN")]
    public sealed class ItemSpecDimensionalController : ExtController
    {
        #region GET/POST Actions

        [HttpPost, PersistState]
        public ActionResult Create(int itemID,
            string sku,
            string weight,
            string length,
            string width,
            string height,
            string[] commonFlags)
        {
            try
            {
                var m = Svc.ProcessOperation("CreateCatalogItemDimensionalSpec", new
                {
                    ItemID = itemID,
                    SKU = sku,
                    Weight = Svc.Weight(weight),
                    Length = Svc.Dimension(length),
                    Height = Svc.Dimension(height),
                    Width = Svc.Dimension(width),
                    CommonFlags = commonFlags.Merge(",")
                });
                return RedirectToAction(Loc.Act("Edit").Ctrl("ItemSpec").Set(new
                {
                    id = JDM.Int32(m.EntityID)
                }));
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(Loc.Act("Create").Ctrl("ItemSpec").Set(new
            {
                code = "DIMENSION"
            }));
        }

        [HttpPost, PersistState]
        public ActionResult Update(int id,
            string sku,
            string weight,
            string length,
            string width,
            string height,
            string[] commonFlags)
        {
            try
            {
                Svc.ProcessOperation("UpdateCatalogItemDimensionalSpec", new
                {
                    EntityID = id,
                    SKU = sku,
                    Weight = Svc.Weight(weight),
                    Length = Svc.Dimension(length),
                    Height = Svc.Dimension(height),
                    Width = Svc.Dimension(width),
                    CommonFlags = commonFlags.Merge(",")
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(Loc.Act("Edit").Ctrl("ItemSpec").Set(new
            {
                id
            }));
        }

        #endregion
    }
}